package demo.practice.dp;

public class P1143 {

    public static void main(String[] args) {
        P1143 p1143 = new P1143();
        p1143.longestCommonSubsequence("abcde", "ace");
    }

    public int longestCommonSubsequence(String text1, String text2) {
        int h = text1.length();
        int w = text2.length();

        int dp[][] = new int[h + 1][w + 1];
        /*for(int i=0;i<h+1;i++){
            dp[i][0]=0;
        }
        for(int j=0;j<w+1;j++){
            dp[0][j]=0;
        }*/
        char[] c1 = text1.toCharArray();
        char[] c2 = text2.toCharArray();
        for (int i = 1; i < h + 1; i++) {
            for (int j = 1; j < w + 1; j++) {
                //c1[i]
                if (c1[i - 1] == c2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                }
            }
        }

        return dp[h][w];
    }
}
